const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
var tMonth = '';
var roomDateMap = new Map();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    uuid: "",
    merchantId: '',
    roomId: '',
    dayId: '',
    priceShow: false,
    dayPriceShow: false,
    radio: '0',
    itemDiscountPrice: '',
    itemPrice: '',
    dayList: [],
    putAwayStatus: true,
    minDate: new Date(2020, 0, 1).getTime(),
    maxDate: new Date(2020, 0, 31).getTime(),
    month: '',
    loadingShow: false,
    formatter(day) {
      // 查询当前月所有月份
      const month = day.date.getMonth() + 1;
      const date = day.date.getDate();
      let dateList = roomDateMap.get(month);
      if (dateList){
        for (var i = 0; i < dateList.length; i++) {
          let dateItem = dateList[i];
          if (month === tMonth) {
            if (date === parseInt(dateItem.dayStr)) {
              if(dateItem.active == 'Y'){
                // 有房
                if(dateItem.payType == 0){
                  day.bottomInfo = '¥' + dateItem.originalPrice;
                } else {
                  day.bottomInfo = '¥' + dateItem.discountPrice;
                }
              } else {
                // 无房
                day.bottomInfo = '无房';
              }
            }
          }
        }
      }
      if (day.type === 'start') {
        day.topInfo = '入住';
      } else if (day.type === 'end') {
        day.topInfo = '离店';
      }
      return day;
    }
  },
  /**日历start */
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    let _that = this;
    let date = event.detail.getDate();
    let month = event.detail.getMonth() + 1;
    let dateTag = month + "-" + date;
    let roomId = _that.data.roomId;
    wx.request({
      url: url + "/HotelSystemApi/queryMonth",
      data: {
        roomId: roomId,
        dateTag: dateTag
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          let result = res.data.data;
          _that.setData({
            dayId: result.id,
            radio: result.payType == 0 ? '0' : '1',
            itemDiscountPrice: result.discountPrice,
            itemPrice: result.originalPrice,
            putAwayStatus: result.active == 'Y' ? true : false,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

    this.setData({
      show: false,
      date: this.formatDate(event.detail)
    });
  },
  /**日历end */
  showDayPricePopup: function(option) {
    let _that = this;
    _that.setData({
      itemPrice: '',
      itemDiscountPrice: ''
    });
    // 加载当前月份所需参数
    let item = option.currentTarget.dataset.item;
    let month = item.month;
    // 以月为单位, 当月之前月的日历按下一年日历展示，否则按今年展示
    let date = new Date();
    let currentMonth = date.getMonth() + 1;
    let year = '';
    if (month < currentMonth) {
      year = date.getFullYear() + 1
    } else {
      year = date.getFullYear()
    }
    let monthId = item.id;
    let uuid = _that.data.uuid;
    // 查询该月份所有天数
    wx.request({
      url: url + "/HotelSystemApi/queryAllDay",
      data: {
        monthId: monthId,
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          let result = res.data.data;
          // 计算天数
          let day = result.length;
          let r = year / 4;
          if (!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
            if (month == 2) {
              day = day - 1;
            }
          }
          _that.setData({
            minDate: new Date(year, month - 1, 1).getTime(),
            maxDate: new Date(year, month - 1, day).getTime()
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
    _that.setData({
      dayPriceShow: true,
    })

    tMonth = month;
  },
  closeDayPricePopup: function() {
    let _that = this;
    _that.setData({
      dayPriceShow: false
    })
  },
  showPricePopup: function(option) {
    let _that = this;
    _that.setData({
      itemPrice: '',
      itemDiscountPrice: ''
    });
    let monthId = option.currentTarget.dataset.monthid;
    let month = option.currentTarget.dataset.month;
    _that.setData({
      priceShow: true,
      monthId: monthId,
      month: month
    })
  },
  closePricePopup: function() {
    let _that = this;
    _that.setData({
      priceShow: false
    })
  },
  updateSiglePrice: function(option) {
    let _that = this;
    let uuid = _that.data.uuid;
    let itemPrice = _that.data.itemPrice;
    let itemDiscountPrice = _that.data.itemDiscountPrice;
    let payType = _that.data.radio;
    let active = _that.data.putAwayStatus;
    let dayId = _that.data.dayId;
    let tempUrl = url + '/HotelSystemApi/editDayPrice';
    let tempData = {
      id: dayId,
      createUserName: uuid,
      originalPrice: itemPrice,
      discountPrice: itemDiscountPrice,
      payType: payType,
      active: active == true ? 'Y' : 'N'
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            dayPriceShow: false
          })
          // 重新加载房源
          _that.queryRoomDate(_that.data.roomId);
          wx.showToast({
            title: '操作成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  update: function(option) {
    let _that = this;
    let monthId = _that.data.monthId;
    let merchantId = _that.data.merchantId;
    let uuid = _that.data.uuid;
    let method = option.currentTarget.dataset.method;
    let roomId = _that.data.roomId;
    let itemPrice = _that.data.itemPrice;
    let itemDiscountPrice = _that.data.itemDiscountPrice;
    let payType = _that.data.radio;
    let active = _that.data.putAwayStatus;
    let month = _that.data.month;
    let tempUrl;
    let tempData;
    tempUrl = url + '/HotelSystemApi/setPriceByMonthId';
    tempData = {
      roomId: roomId,
      originalPrice: itemPrice,
      merchantId: merchantId,
      createUserName: uuid,
      discountPrice: itemDiscountPrice,
      payType: payType,
      monthId: monthId,
      active: active == true ? 'Y' : 'N'
    }
    wx.request({
      url: tempUrl,
      data: tempData,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            priceShow: false
          })
          wx.showToast({
            title: month + '月设置成功!',
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  /**表单 start */
  onPutAwayStatusChange: function(event) {
    let _that = this;
    _that.setData({
      putAwayStatus: event.detail
    });
  },
  onKeyChange: function(option) {
    let _that = this;
    _that.setData({
      key: option.detail
    })
  },
  onValueChange: function(option) {
    let _that = this;
    _that.setData({
      value: option.detail
    })
  },
  /**
   * 删除
   */
  deleteUnit: function(option) {
    let _that = this;
    let id = _that.data.id;
    let uuid = _that.data.uuid;
    let merchantId = _that.data.merchantId;
    let descType = _that.data.descType;
    let detailId = _that.data.detailId;
    let keyType = _that.data.keyType;
    wx.request({
      url: url + '/HotelSystemApi/deleteKeyValue',
      data: {
        id: id,
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            show: false
          })
          _that.queryData(res.data, merchantId, descType, keyType, detailId);
          wx.showToast({
            title: "删除成功!",
            icon: "success",
            duration: 2000
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },
  /**表单 end */
  /**
   * 结算类型
   */
  onRadioChange: function(event) {
    let _that = this;
    _that.setData({
      radio: event.detail
    });
  },
  /**
   * 折扣价
   */
  onDiscountPriceChange: function(event) {
    let _that = this;
    _that.setData({
      itemDiscountPrice: event.detail
    });
  },

  /**
   * 原价
   */
  onPriceChange: function(event) {
    let _that = this;
    _that.setData({
      itemPrice: event.detail
    });
  },
  openUnitPop: function(option) {
    let _that = this;
    let type = option.currentTarget.dataset.type;
    if (type == 1) {
      let key = option.currentTarget.dataset.key;
      let value = option.currentTarget.dataset.value;
      let id = option.currentTarget.dataset.id;
      _that.setData({
        id: id,
        key: key,
        value: value,
      })
    }
    _that.setData({
      btnType: type,
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onLoad: function(options) {
    let _that = this;
    let merchantId = options.merchantId;
    let roomId = options.roomId;
    console.log(options);
    wx.setNavigationBarTitle({
      title: options.title
    })
    wx.getStorage({
      key: "uuid",
      success(res) {
        console.log(res);
        _that.setData({
          uuid: res.data
        })
        _that.queryRoomDate(roomId);
        _that.queryData(res.data, merchantId, roomId);
      }
    })
    _that.setData({
      merchantId: merchantId,
      roomId: roomId
    })
  },
  queryRoomDate: function(roomId){
    // 加载页
    let _that = this;
    _that.setData({
      loadingShow:true
    })
    wx.request({
      url: url + "/HotelSystemApi/queryAllMonthDayPrice",
      data: {
        roomId: roomId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          let result = res.data.data;
          roomDateMap = new Map();
          for (var i = 0; i < result.length; i++) {
            roomDateMap.set(i + 1, result[i]);
          }
          // 
          _that.setData({
            loadingShow: false
          })
          console.log(roomDateMap);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      }
    })
  },
  queryData: function(uuid, merchantId, roomId) {
    let _that = this;
    wx.request({
      url: url + '/HotelSystemApi/queryAllMonth',
      data: {
        uuid: uuid,
        merchantId: merchantId,
        roomId: roomId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          _that.setData({
            unitList: res.data.data
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },
  changeVal: function(e) {
    let _that = this;
    _that.setData({
      value: e.detail
    })
  },



})